#include "widget.h"
#include "ui_widget.h"
#include<QPainter>
#include<QTimer>
#include<QKeyEvent>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget),blsrun(false),speed(500)
{
    ui->setupUi(this);
    this->setGeometry(QRect(900,300,300,300));
}

Widget::~Widget()
{
    delete ui;
}
void Widget::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!blsrun)
    initsnake();
    //画游戏背景
    //外墙
    painter.setPen(Qt::black);//设置画笔颜色
    painter.setBrush(Qt::gray);//设置填充颜色
    painter.drawRect(5,5,290,290);
    //内墙
    painter.setPen(Qt::black);//设置画笔颜色
    painter.setBrush(Qt::black);//设置填充颜色
    painter.drawRect(10,10,280,280);
    painter.drawPixmap(10,10,280,280,QPixmap(":/image/kkt.jpg"));
    //画格子
    painter.setPen(Qt::blue);
    for(int i=2;i<=29;i++){
        painter.drawLine(10,i*10,290,i*10);//画横线
        painter.drawLine(i*10,10,i*10,290);//画竖线
    }
    //显示游戏开始和结束
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(40,150,display);
    //显示分数
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(180,25,"得分：");
    painter.drawText(250,25,QString::number(score));
    //画蛇
    painter.setPen(Qt::black);
    painter.setBrush(Qt::yellow);
    //painter.drawRect(vsnakeRect);
    painter.drawPixmap(snakehead,QPixmap(":/image/kk.png"));
    painter.drawRect(food);
    painter.drawPixmap(food,QPixmap(":/image/R-C.jpg"));
    painter.drawRects(&vsnakeRect[0],vsnakeRect.size());
    if(blsover)
        timer->stop();
}
//蛇函数
void Widget::initsnake(){
    blsrun=true;
    blsover=false;
    ndirection='s';
    display="游戏开始！只因！";
    score=0;
    food=createfood();
    //QRect rect(110,80,10,10);
    //vsnakeRect=rect;
    vsnakeRect.resize(5);//蛇的长度
    for(int i=0;i<vsnakeRect.size();i++){
        QRect rect(150,100+10*i,10,10);
        vsnakeRect[vsnakeRect.size()-1-i]=rect;
    }
    timer=new QTimer(this);//设定计时器
    timer->start(speed);//设定时间500ms
    connect(timer,SIGNAL(timeout()),SLOT(snakeupdate()));

}
void Widget::snakeupdate(){
    display=" ";
    snakehead=vsnakeRect.first();//获取蛇头
    iseat();
    ishit();
    for(int j=0;j<vsnakeRect.size()-1;j++)
        vsnakeRect[vsnakeRect.size()-1-j]=vsnakeRect[vsnakeRect.size()-2-j];
    switch(ndirection){
    case 'w':
        snakehead.setTop(snakehead.top()-10);
        snakehead.setBottom(snakehead.bottom()-10);
        break;
    case 's':
        snakehead.setTop(snakehead.top()+10);
        snakehead.setBottom(snakehead.bottom()+10);
        break;
    case 'a':
        snakehead.setLeft(snakehead.left()-10);
        snakehead.setRight(snakehead.right()-10);
        break;
    case 'd':
        snakehead.setLeft(snakehead.left()+10);
        snakehead.setRight(snakehead.right()+10);
        break;
    default:;
    }
    vsnakeRect[0]=snakehead;
    if(snakehead.left()<10||snakehead.right()>280||snakehead.top()<10||snakehead.bottom()>280){
            blsover=true;
    display="游戏结束!你干嘛哎哟！";}
    update();//更新
}
void Widget::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*) event;
    switch(key->key()){
    case Qt::Key_Up: ndirection='w';
        break;
    case Qt::Key_Down:ndirection='s';
        break;
    case Qt::Key_Left: ndirection='a';
        break;
    case Qt::Key_Right: ndirection='d';
        break;
    default:;
    }
}
QRect Widget::createfood(){
    int x,y;
    x=qrand()%25;
    y=qrand()%25;
    QRect rect(20+x*10,20+y*10,10,10);
    return rect;
}
void Widget::iseat(){
    if(snakehead==food){
        snakehead=food;
        vsnakeRect.push_back(vsnakeRect.last());
        food=createfood();
        score=score+10;
        speed=speed-10;
        timer->stop();//停止当前计时器
        timer->start(speed);
    }
}
void Widget::ishit(){
    for(int i=1;i<vsnakeRect.size();i++){
        if(snakehead==vsnakeRect[i]){
            display="游戏结束！你干嘛哎哟！";
            blsover=true;
            update();
        }
    }
}
